package com.unicom.priceboard.thread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;

import com.unicom.priceboard.data.hose.FRoomReader;
import com.unicom.priceboard.data.vo.FRoomElement;
import com.unicom.priceboard.util.Constants;
import com.unicom.priceboard.util.DataPath;
import com.unicom.priceboard.util.ReadFileUtil;
import com.unicom.priceboard.util.Util;

public class FroomThread  implements Runnable{
	private int wait=1;
	public static volatile HashMap<Integer, FRoomElement> lstFRoom = new HashMap<Integer, FRoomElement>();
	public static volatile HashMap<Integer, FRoomElement> oldFRoom = new HashMap<Integer, FRoomElement>();
	//path of DAT file
	public static boolean froomFinish=false;
	
	private static String fRoomPath = "";
	private static String oldRoom = "";
	
	private boolean readFroom=true;
	private int curday=-1;
	private int prevday=-1;
	private FRoomReader parseFroomReader=new FRoomReader();
	public FroomThread(){
		this.wait=Integer.parseInt(DataPath.hoseWait); //seconds
		fRoomPath = DataPath.basePath+ DataPath.fRoomPath;
		oldRoom = DataPath.oldRoom; 
	}
		
	public void run() {
		while(true){
			try{
				if(TimerThread.CURRENT_DAY_OF_WEEK!=1 && TimerThread.CURRENT_DAY_OF_WEEK!=7) {	
					if(TimerThread.CURRENT_TIME>=700 && TimerThread.CURRENT_TIME<1130){
							if(ReadFileUtil.isUpToDate(Util.getCurrentDataPath(fRoomPath))){
								    froomFinish=false;
									curday=TimerThread.CURRENT_DAY_OF_WEEK;
									if(curday!=prevday && readFroom){
										lstFRoom.clear();
										oldFRoom.clear();
									    if(TimerThread.CURRENT_TIME>830 && TimerThread.CURRENT_TIME<850){
										    	byte data[] = new byte[4096];
										        int c;
										        FileInputStream fis = new FileInputStream(Util.getCurrentDataPath(fRoomPath));
										        FileOutputStream fos = new FileOutputStream(Util.getCurrentDataPath(oldRoom));
										        while ((c = fis.read(data)) != -1)
											        fos.write(data, 0, c);
										        
											        fis.close();
											        fos.flush();
											        fos.close();
											        readFroom=false;
										 }
									    if(ReadFileUtil.isUpToDate(Util.getCurrentDataPath(oldRoom))){
									    	parseFroomReader.parseFroom(oldRoom, Constants.FROOMOLD);
									    	prevday=curday;
									    }										   
									}   
									//read new Froom
									parseFroomReader.parseFroom(fRoomPath, Constants.FROOMNEW);
									froomFinish=true;
								}
					}else if(lstFRoom.size()==0){
							froomFinish=false;						
							File checkfile =new File(Util.getCurrentDataPath(oldRoom));
						    if(checkfile.exists()){
						    	parseFroomReader.parseFroom(oldRoom, Constants.FROOMOLD);
						    }						
						parseFroomReader.parseFroom(fRoomPath, Constants.FROOMNEW);
						froomFinish=true;
					}					
				}else if(lstFRoom.size()==0){
					froomFinish=false;					
					File checkfile =new File(Util.getCurrentDataPath(oldRoom));
				    if(checkfile.exists()){
				    	parseFroomReader.parseFroom(oldRoom, Constants.FROOMOLD);
				    }											
				    parseFroomReader.parseFroom(fRoomPath, Constants.FROOMNEW);
				    froomFinish=true;
				}
			}catch (Exception e) {
			}			
//				wait a period before continue to read
			try {
				Thread.sleep(1000*wait);
			} catch (Exception e) {
				//logger.debug("Exception: " + e.getMessage());
			}
		}
	}
}
